<html>
    <body>
        <button id="button">点击</button>
    </body>
    <script>
        let MacroCommand = function() {
            return {
                commandList: [],
                add: function(command) {
                    this.commandList.push(command);
                },
                execute: function() {
                    for (let i = 0; i < this.commandList.length; i++) {
                        const command = this.commandList[i];
                        command.execute();
                    }
                }
            }
        }

        const openAcCommand = {
            execute: () => {
                console.log('开空调');
            }
        }

        // 电视和音响的开关在一起

        const openTVCommand = {
            add: () => {
                throw new Error('不能添加子节点');
            },
            execute: () => {
                console.log('开电视');
            }
        }

        const openSoundCommand = {
            execute: () => {
                console.log('开音响');
            }
        }

        const macro1 = MacroCommand();
        macro1.add(openTVCommand);
        macro1.add(openSoundCommand)

        // 关门 开电脑 开微信在一起

        let closeDoorCommand =  { // 智能命令
            execute: function () {
                console.log('关门');
            }
        }

        let openComputerCommand = {
            execute: function() {
                console.log('开电脑');
            }
        }

        let openWXCommand = {
            execute: function() {
                console.log('打开微信');
            }
        }

        const macro2 = MacroCommand();
        macro2.add(closeDoorCommand);
        macro2.add(openComputerCommand);
        macro2.add(openWXCommand);

        const macro = MacroCommand();
        macro.add(openAcCommand);
        macro.add(macro1);
        macro.add(macro2);

        const setCommand = (function(command) {
            console.log(command.execute());
        })(macro)
    </script>
</html>
